package com.zhz.company.excel;

import com.zhz.common.tool.bigdata.excel.BaseEasyExcelExport;
import com.zhz.common.tool.mybatisplus.base.core.contract.Page;
import com.zhz.common.tool.mybatisplus.base.core.contract.PageParam;
import com.zhz.company.repository.service.UserDemoService;
import com.zhz.model.demo.contract.dto.UserDemoDTO;
import com.zhz.model.demo.contract.param.UserDemoQueryParam;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * @author zhouhengzhe
 * @date 2023/4/6
 */
@Component
public class UserExcelExport extends BaseEasyExcelExport<UserDemoQueryParam> {
    @Autowired
    private UserDemoService userDemoService;


    /**
     * 数据导出
     */
    public void exportWithBigData(String fileName, UserDemoQueryParam userDemoQueryParam, HttpServletResponse response) {
        this.exportExcel(fileName, userDemoQueryParam,response);
    }

    /**
     * 获取导出的表头
     */
    @Override
    protected List<List<String>> getExcelHead() {
        List<List<String>> head = new ArrayList<>();
        head.add(Collections.singletonList("ID"));
        head.add(Collections.singletonList("用户ID"));
        head.add(Collections.singletonList("用户姓名"));
        head.add(Collections.singletonList("创建时间"));
        head.add(Collections.singletonList("更新时间"));
        return head;
    }

    /**
     * 计算导出数据的总数
     *
     * @param userDemoQueryParam 查询参数
     */
    @Override
    protected Long dataTotalCount(UserDemoQueryParam userDemoQueryParam) {
        //return userDemoQueryParam.getLimit();
        return (long)userDemoService.count(userDemoQueryParam);
    }

    /**
     * 每一个sheet存放的数据总数
     */
    @Override
    protected Long eachSheetTotalCount() {
        return 5000L;
    }

    /**
     * 每次写入sheet的总数
     */
    @Override
    protected Long eachTimesWriteSheetTotalCount() {
        return 2000L;
    }

    /**
     * 构建每次查询数量
     *
     * @param resultList
     * @param userDemoQueryParam
     * @param pageNo
     * @param pageSize
     */
    @Override
    protected void buildDataList(List<List<String>> resultList, UserDemoQueryParam userDemoQueryParam, Long pageNo, Long pageSize) {
        PageParam<UserDemoQueryParam> pageParam = new PageParam<>();
        pageParam.setPage(pageNo);
        pageParam.setLimit(pageSize);
        pageParam.setParam(userDemoQueryParam);
        Page<UserDemoDTO> page = userDemoService.page(pageParam);
        if (CollectionUtils.isNotEmpty(page.getList())) {
            page.getList().forEach(userDemoDTO -> {
                LocalDateTime createTime = userDemoDTO.getCreateTime();
                LocalDateTime updateTime = userDemoDTO.getUpdateTime();
                DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
                String createTimeStr = createTime.format(df);
                String updateTimeStr = updateTime.format(df);
                //按照顺序填充
                resultList
                        .add(Arrays.asList(userDemoDTO.getId(),
                                userDemoDTO.getUserId(),
                                userDemoDTO.getUserName(),
                                createTimeStr,
                                updateTimeStr));
            });
        }
    }
}
